<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header class="el_header">
      <div>
        <img
          src="../assets/heima.png"
          alt
        >
        <span>后台管理系统 v1.0</span>
      </div>
      <el-button
        type="danger"
        size="small"
        round
        icon="el-icon-switch-button
"
        @click="logout"
      >
        退出
      </el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 左侧区域 -->
      <el-aside
        :width="isToggle?'69px':'200px'"
        class="el_aside"
      >
        <div
          class="toggle-button"
          @click="toggleCollapse"
        >
          <i class="el-icon-more" />
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eef"
          unique-opened
          :collapse="isToggle"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]" />
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/' + subItem.path"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu" />
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main class="el_main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // id对应的类名
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isToggle: false,
      // 路由地址
      activePath: ''
    }
  },
  created() {
    // 加载数据
    this.getMenuList()
    // 取出sessionStorage存储的状态
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      // 清空 sessionStorage 里的信息 token
      window.sessionStorage.clear()
      // 跳转到 login 页
      this.$router.push('/login')
      this.$message.info('你已退出登录')
    },
    async getMenuList() {
    //  获取菜单数据
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    //  console.log(res)
    },
    // 切换侧边栏的显示大小
    toggleCollapse() {
      this.isToggle = !this.isToggle
    },
    // 存储相应的路由地址
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
  .el_header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;

    >div{
      font-size: 20px;
      color: #fff;
      display: flex;
      align-items: center;
      img{
        background-color: #ddd;
        border-radius: 50%;
        width:45px;
      }
      span{
        margin-left: 10px;
      }
    }
  }
  .el_aside{
    background-color: #333744;
    .el-menu{
      border-right:0;
    }
  }
  .el-main{
    background-color: #eaedf1;
  }
  .home_container{
    height: 100%;
  }
  .el-submenu span{margin-left: 10px;}
  .toggle-button{
    color: #fff;line-height: 24px;font-size: 20px;text-align: center;background-color: #4a5064;
    cursor: pointer;
    }
</style>
